package com.cndym.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.cndym.adapterForMobile.GlobalMobileAdapter;
import com.cndym.exception.CndymException;
import com.cndym.exception.ErrCode;
import com.cndym.exception.MsgException;
import com.cndym.utils.JsonBinder;
import com.cndym.utils.Utils;

/**
 * 业务逻辑总入口
 */
public class ClientInterfaceServlet extends HttpServlet {

	private static final long serialVersionUID = 1876611800545408319L;
	private Logger logger = Logger.getLogger(getClass());

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/json; charset=UTF-8");
		long time = new Date().getTime();
		PrintWriter out = response.getWriter();
		String msg = Utils.formatStr(request.getParameter("msg"));
		if("TokenExpires".equals(msg)) //此值在ChangeRequestWarpper定义
		{
			Map<String, String> map=new HashMap<String, String>();
			map.put("code", ErrCode.E1400);
			map.put("msg", ErrCode.codeToMsg(ErrCode.E1400));
			out.print(JsonBinder.buildNonDefaultBinder().toJson(map));
			return;
		}
		if("LoginOnOtherMachine".equals(msg))
		{
			Map<String, String> map=new HashMap<String, String>();
			map.put("code", ErrCode.E1401);
			map.put("msg", ErrCode.codeToMsg(ErrCode.E1401));
			out.print(JsonBinder.buildNonDefaultBinder().toJson(map));
			return;
		}
		
		final String ip = Utils.getIpAddress(request);
		String type ="json";//此版本只有json格式,可以为常量
		GlobalMobileAdapter globalMobileAdapter = new GlobalMobileAdapter();
		Map<String, String> reMap = new HashMap<String, String>();
		String reBody = null;
		try {
			
			reBody = globalMobileAdapter.execute(type, msg, request);

		} catch (CndymException e) {
			logger.error(ErrCode.codeToMsg(e.getMessage()), e);
			reMap.put("code", e.getMessage());
			reMap.put("msg", ErrCode.codeToMsg(e.getMessage()));
			reBody = JsonBinder.buildNonDefaultBinder().toJson(reMap);
		} catch (MsgException e) {
			logger.error(ErrCode.E9999, e);
			reMap.put("code", ErrCode.E9999);
			reMap.put("msg", e.getMessage());
			reBody = JsonBinder.buildNonDefaultBinder().toJson(reMap);
		} catch (Exception e) {
			logger.error(ErrCode.E9999, e);
			reMap.put("code", ErrCode.E9999);
			reMap.put("msg", ErrCode.codeToMsg(ErrCode.E9999));
			reBody = JsonBinder.buildNonDefaultBinder().toJson(reMap);
		}
		long subTime = new Date().getTime() - time;
		
		logger.info("ip=" + ip + "," + reBody + ";msg=" + msg + ",执行时间(" + (subTime) + ")毫秒");
		out.print(reBody);
		out.flush();
		out.close();
	}
	
	
	protected void doGet(javax.servlet.http.HttpServletRequest request,
			javax.servlet.http.HttpServletResponse response)
			throws javax.servlet.ServletException, IOException {
		doPost(request, response);
	}
	
	
}
